/* topmenuvel kapcsolatos var-k */
// kesobbiekben jol johet, ha true-ra rakjuk akkor minden menu kozepre lesz
// igazitva, ha false akkor a ket szelso menu jobbra ill balra fog tapadni
var allMenuAlignCenter = true;
// almenu eltolasa x-n
var submenuOffsetX = 0;
// almenu eltolasa y-n
var submenuOffsetY = 32;

/* ---------------------------- */

var l1ActiveMenu = null;
var l1ActiveMenuId = -1;
var visibleMenuCount = null;



var hideTimerIDs = new Array();
for(var i=0; i < visibleMenuCount; i++) {
	var id = topMenu.menuItemIds[i];
	hideTimerIDs[id] = -1;
}

var displayedSubmenusOfMenuId = null;
var showSubmenusOfMenuIdAfterHide = null;


function l2RollOverEvent(id) {
	$('topmenu_submenu_td_'+id).setStyle({backgroundColor: '#d1d3d4'});
}


function l2RollOutEvent(id) {
	$('topmenu_submenu_td_'+id).setStyle({backgroundColor: '#e9f0e8'});	
}


function menuIsActive(menuId) {
	var menu = topMenu.findMenu(menuId);
	if(menu != null)
	{
		if(menu.level == 1)
		{
			l1ActiveMenu = menu;
		}
		if(menu.level == 2)
		{
			l1ActiveMenu = menu.parent;
		}
		l1ActiveMenuId = l1ActiveMenu.id;
		showMenuSubmenus(l1ActiveMenuId);
	}
}

function l1MenuItemMouseEnter(id) {
	var menu = topMenu.findMenu(id);
	if(menu == null)
		return;
	
	// $('topmenuitem_span_'+id).setStyle({fontWeight: 'bold'});
	
	if(!menu.hasChildren())
		return;
	
	activeSubmenu = true;
	showMenuSubmenus(id);
}

function l1MenuItemMouseLeave(id) {
	// aktiv menunek nincs leave-je
	if(l1ActiveMenuId == id)
		return;
	
	var menu = topMenu.findMenu(id);
	if(menu == null)
		return;
	if(!menu.hasChildren()) {
		// $('topmenuitem_span_'+id).setStyle({fontWeight: 'normal'});
		return;
	}
	
	hideMenuSubmenus(id);
	
}

function showMenuSubmenus(id) {
	if(id == null)
		return;
	
	_shutdownHideTimer(id);	
	
	if(displayedSubmenusOfMenuId == id)
		// nincs dolgunk
		return;
	
	// azonnal kikapcs ami kint van
	hideMenuSubmenus(displayedSubmenusOfMenuId, true);
	
	// bekapcs a mostani
	$('topMenuSubmenuLevel2_'+id).setStyle({display: 'block'});
	
	// megnezi, hogy milyen szeles az elso szintu menuje
	var level1MenuWidth = $('topmenulabel_div_'+id).getWidth();
		
	var menu = topMenu.findMenu(id);
		
	var longestLabelWidth = 0;
	// megnezi hogy melyik a leghosszabb menu label
	for (var i = 0; i < menu.childrenIds.length; i++) {
		$('topmenu_submenu_'+menu.childrenIds[i]).setStyle({whiteSpace: 'nowrap'});
		var currentLabelWidth = $('topmenu_submenu_'+menu.childrenIds[i]).getWidth();
		
		longestLabelWidth = (longestLabelWidth > currentLabelWidth) ? longestLabelWidth : currentLabelWidth;
		
		$('topmenu_submenu_'+menu.childrenIds[i]).setStyle({whiteSpace: 'normal'});		
	}
	
	// azert adunk hozza 16-t, mert az almenu padding-left 16px
	longestLabelWidth += 16;
	
	// ha rovidebb, mint a elso szintu menu szelesseg, akkor kinyomjuk akkorara
	if (longestLabelWidth < level1MenuWidth)
		longestLabelWidth = level1MenuWidth;
	
	// ha nagyobb, mint a max szelesseg, akkor kisebbre nyomjuk
	if (longestLabelWidth > topMenuMaxSubmenuWidth)
		var newWidth = topMenuMaxSubmenuWidth;
	else
		var newWidth =  longestLabelWidth;
	
	$('topMenuSubmenuLevel2Table_'+id).setStyle({width: newWidth+'px'});
	
	var menuOffset = $('topmenulabel_div_'+id).cumulativeOffset();
	var menuWidth = $('topmenulabel_div_'+id).getWidth();
		
	var xoffset = menuOffset[0] + submenuOffsetX;
	var yoffset = menuOffset[1] + submenuOffsetY;
		
	var menuContainerOffset = $('topMenuTableId').cumulativeOffset();
	
	// ki kell szamolni, hogy kilog e, ha igen, akkor a menu jobb oldalahoz kell igazitani a submenu jobb oldalat
	if (menuContainerOffset[0] + topMenuWidth < xoffset + newWidth) {				
		xoffset -= newWidth - menuWidth + submenuOffsetX;
	}
	$('topMenuSubmenuLevel2_'+id).setStyle({display: 'block'});
	$('topMenuSubmenuLevel2_'+id).setStyle({position: 'absolute', left: xoffset+'px', top: yoffset+'px', zIndex:'1'});
	
	// most mar ez latszik
	displayedSubmenusOfMenuId = id;
	
}

function hideMenuSubmenus(id, doNotDelay) {
	if(id == null)
		return;
	
	_shutdownHideTimer(id);

	if(displayedSubmenusOfMenuId != id)
		// ez nincs kint, nincs dolgunk
		return;
	
	if(doNotDelay)
	{
		showSubmenusOfMenuIdAfterHide = null;
		doHideMenuSubmenus(id);
		activeSubmenu = false;
		return;
	}
	
	if(showSubmenusOfMenuIdAfterHide == null && id != l1ActiveMenuId && l1ActiveMenu != null)
		showSubmenusOfMenuIdAfterHide = l1ActiveMenuId;
	
	hideTimerIDs[id] = window.setTimeout("doHideMenuSubmenus("+id+")", 500);
	
}

function doHideMenuSubmenus(id) {
	hideTimerIDs[id] = -1;
	
	// $('topmenuitem_span_'+id).setStyle({fontWeight: 'normal'});
	
	$('topMenuSubmenuLevel2_'+id).setStyle({display: 'none'});
	displayedSubmenusOfMenuId = null;
	
	if(showSubmenusOfMenuIdAfterHide != null)
	{
		showMenuSubmenus(showSubmenusOfMenuIdAfterHide);
		showSubmenusOfMenuIdAfterHide = null;
	}
	activeSubmenu = false;
}

function _shutdownHideTimer(id) {
	if(hideTimerIDs[id] != -1)
	{
		// lelojuk a hide timert!
		window.clearTimeout(hideTimerIDs[id]);
	}
	hideTimerIDs[id] = -1;
}
